package sliver.action.shop.ajax;

import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sliver.entity.Advertisement;
import sliver.service.shop.ads.AdvertisementService;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller("shop.ajax.queryAdvertisementAction")
public class QueryAdvertisementAction extends ActionSupport{

	private static final long serialVersionUID = 6214634475017306926L;
	
	private Integer shopperId;
	private Long machieId;
	private Integer position;
	private Integer ranking;
	private Long customId;
	
	@Autowired
	private AdvertisementService advertisementService;
	
	public QueryAdvertisementAction(){
		
	}

	public Integer getShopperId() {
		return shopperId;
	}

	public void setShopperId(Integer shopperId) {
		this.shopperId = shopperId;
	}
	
	public Long getMachieId() {
		return machieId;
	}

	public void setMachieId(Long machieId) {
		this.machieId = machieId;
	}

	public Integer getPosition() {
		return position;
	}

	public void setPosition(Integer position) {
		this.position = position;
	}

	public Integer getRanking() {
		return ranking;
	}

	public void setRanking(Integer ranking) {
		this.ranking = ranking;
	}
	
	public Long getCustomId() {
		return customId;
	}

	public void setCustomId(Long customId) {
		this.customId = customId;
	}

	@Override
	public String execute() throws Exception {
		
		List<Advertisement> advertisements = null;
		
		if(customId != null){
			advertisements = advertisementService.matchingAdvertisementByKeywords(shopperId, machieId, customId, position, ranking);
		}else{
			advertisements = advertisementService.matchingAdvertisement(shopperId,machieId, position, ranking);
		}
		
		StringBuilder json = new StringBuilder();
		json.append("[");
		if(advertisements != null && advertisements.size() > 0){
			for(Advertisement advertisement : advertisements){
				json.append("{")
					.append(JSON.toJSONString("id")).append(":").append(JSON.toJSONString(advertisement.getId())).append(",")
					.append(JSON.toJSONString("imagePath")).append(":").append(JSON.toJSONString(advertisement.getImagePath())).append(",")
					.append(JSON.toJSONString("link")).append(":").append(JSON.toJSONString(advertisement.getLink()))
					.append("}")
					.append(",");
			}
			json = json.deleteCharAt(json.length() - 1);
		}
		json.append("]");
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
		
		Writer writer = response.getWriter();
		writer.write(json.toString());
		writer.flush();
		
		return null;
	}
}
